import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Sky extends World{
    private Lifes l;  // Counter of the lifes availables.
    private Man man;
    private int punt;
    private int energia;
    private int puntaje;
    private Lifes score;
    private boolean back;
    private UserInfo user;
    private Bird bird = new Bird(1);
    private Home home = new Home(1);
    private Grass grass1 = new Grass(1);                 //Scenario's grass.
    private Grass grass2 = new Grass(1);
    private Apple apple1 = new Apple(1);                  //Accesories.
    private Apple apple2 = new Apple(1);
    private Apple apple3 = new Apple(1);
    private Apple apple4 = new Apple(1);
    private Apple apple5 = new Apple(1);
    private Poison poison1 = new Poison(1);
    private Poison poison2 = new Poison(1);
    private Poison poison3 = new Poison(1);
    private Poison poison4 = new Poison(1);
    private Poison poison5 = new Poison(1);
    private Poison poison6 = new Poison(1);
    private JumpShoes shoes = new JumpShoes(1);
    private Mountain mountain = new Mountain(1);
    private Tree tree1 = new Tree("Arb_01.gif", 1);        //Scenario's trees.
    private Tree tree2 = new Tree("Arb_02.gif", 1);
    private Tree tree3 = new Tree("Arb_03.gif", 1);
    private Tree tree4 = new Tree("Arb_04.gif", 1);
    private Tree tree5 = new Tree("Arb_05.gif", 1);
    private Tree tree6 = new Tree("Arb_06.gif", 1);
    private Tree tree7 = new Tree("Arb_07.gif", 1);
    private Tree tree8 = new Tree("Arb_08.gif", 1);
    private Tree tree9 = new Tree("Arb_09.gif", 1);

    /**
     * Sky Constructor. Create a new world with 600x500 cells with a cell size of 1x1 pixels. 
     */
    public Sky()
    {        
        super( 600, 500, 1, false); 
        puntaje=0;
        energia=300;
        man = new Man(1); 
        score= new Lifes(0, "Score = ");
        l = new Lifes( 5, "  Lifes = "); 
        prepare();
    }

    public void act(){
        checkFlagP( poison1);
        checkFlagP( poison2);
        checkFlagP( poison3);
        checkFlagP( poison4);
        checkFlagP( poison5);
        checkFlagP( poison6);
        checkFlagA( apple1);
        checkFlagA( apple2);
        checkFlagA( apple3);
        checkFlagA( apple4);
        checkFlagA( apple5);
        checkFlagJS( shoes);
        if( energia == 0)
        {
            energia = 200;
            l.decrementa();
            if( l.getNumLifes() == 0)
            {                              //It shows on the screen "GAME OVER" when the player lost.
                Message lose = new Message( "**GAME OVER**");
                addObject( lose, 250,150);
                Greenfoot.delay( 50);
                Greenfoot.setWorld(new Menu(0) );
            }
        }
        score.incrementa( puntaje() - punt);
        punt = puntaje();
    }

    /*** Prepare the world for the start of the program. That is: create the initial objects and add them to the world.*/
    private void prepare()
    {
        addObject( grass1, 300, 450);
        addObject( grass2, getWidth(), 450);
        addObject( tree1, 66, 380);
        addObject( tree2, 132, 330);
        addObject( tree3, 198, 370);
        addObject( tree4, 264, 280);
        addObject( tree5, 330, 340);
        addObject( tree6, 396, 310);
        addObject( tree7, 462, 370);
        addObject( tree8, 528, 280);
        addObject( tree9, 600, 335);
        addObject( home, 3200, 110);
        addObject( apple1, 588, 271);
        addObject( apple2, 332, 275);
        addObject( apple3, 466, 330);
        addObject( apple4, 190, 400);
        addObject( apple5, 571, 159);
        addObject( poison1, 80, 390);
        addObject( poison2, 730,263);
        addObject( poison3, 1013, 315);
        addObject( poison4, 487, 331);
        addObject( poison5, 290, 402);
        addObject( poison6, 277, 152);
        addObject( shoes, 323, 411);
        addObject( mountain, 3100, 315);
        addObject( score, 50, 30);
        addObject( bird, 540, 50);
        addObject( l, 550, 470);
        addObject( man, 50, 400);
        man.setLocation( 50,374); 
    }

    /**
     * Methods of control.
     */
    public Man getMan()
    {
        return man;
    }

    public Bird getBird()
    {
        return bird;
    }

    public Lifes getLifes()
    {
        return l;
    }

    public int level()
    {
        return 1;
    }

    /**
     * Those methods manage the energy.
     */
    public void checkFlagP( Poison pX)
    {
        if( man.toxic( pX) == true)
        {
            energia = energia - 50;
            removeObject( pX);
        }
    } 

    public void checkFlagA( Apple aX)
    {
        if( man.food( aX) == true){
            energia = energia + 50;
            removeObject( aX);
        }
    }

    public void checkFlagJS( JumpShoes jsX)
    {
        if( man.wear(jsX) == true)
            removeObject( jsX);
    }

    /**
     * Method puntaje returns the distance to use it like score.
     */
    public int puntaje()
    {
        if( bird.punt()>puntaje)
            puntaje = bird.punt();
        return puntaje;
    }
}
